home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume7 / sherror < prev    next >
Encoding:
Text File  |  1989-08-06  |  5.1 KB  |  191 lines

  1. Newsgroups: comp.sources.misc
  2. subject: v07i121: Some simple but useful shell script tools
  3. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  4. Reply-To: frew%crseo@hub.ucsb.edu
  5.  
  6. Posting-number: Volume 7, Issue 121
  7. Submitted-by: frew%crseo@hub.ucsb.edu
  8. Archive-name: sherror
  9.  
  10. Here are a couple of tools I use a LOT for writing shell scripts that
  11. will be around for a while.  These tools let shell scripts generate
  12. standard usage and error messages just like real programs (should).
  13. A sample "production" script is included that demonstrates their usage.
  14.  
  15. #! /bin/sh
  16. # This is a shell archive, meaning:
  17. # 1. Remove everything above the #! /bin/sh line.
  18. # 2. Save the resulting text in a file.
  19. # 3. Execute the file with /bin/sh (not csh) to create the files:
  20. #    README
  21. #    sherror
  22. #    usage
  23. #    xch
  24. # This archive created: Sun Aug  6 14:29:31 1989
  25. # By:    Jim Frew ()
  26. export PATH; PATH=/bin:$PATH
  27. echo shar: extracting "'README'" '(615 characters)'
  28. if test -f 'README'
  29. then
  30.     echo shar: will not over-write existing file "'README'"
  31. else
  32. sed 's/^X//' << \SHAR_EOF > 'README'
  33. X06-Aug-1989
  34. X
  35. XHere are a couple of trivial utilities that make writing shell scripts
  36. Xa lot easier.
  37. X
  38. X"usage" writes a usage message to the standard error output.
  39. X
  40. X"sherror" writes an error message to the standard error output.  If you
  41. Xmake a link to sherror named "shbug", then the message says "BUG"
  42. Xinstead of "ERROR".
  43. X
  44. XA simple script "xch" is included that demonstrates both usage and
  45. Xsherror.
  46. X
  47. X#-----------------+-----------------------+-----------------------------
  48. X# James Frew      | frew@crseo.ucsb.edu   | Computer Systems Lab., UCSB
  49. X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA
  50. SHAR_EOF
  51. if test 615 -ne "`wc -c < 'README'`"
  52. then
  53.     echo shar: error transmitting "'README'" '(should have been 615 characters)'
  54. fi
  55. fi # end of overwriting check
  56. echo shar: extracting "'sherror'" '(659 characters)'
  57. if test -f 'sherror'
  58. then
  59.     echo shar: will not over-write existing file "'sherror'"
  60. else
  61. sed 's/^X//' << \SHAR_EOF > 'sherror'
  62. X#!/bin/sh
  63. X
  64. X# sh{error,bug} -- print shell script {error,bug} message
  65. X
  66. Xexec 1>&2
  67. X
  68. Xcase $0 in
  69. X*bug)    severity=BUG
  70. X    ;;
  71. X*)    severity=ERROR
  72. X    ;;
  73. Xesac
  74. X
  75. Xpgm=`basename ${1:-command?}`
  76. Xmsg="${2:-message?}"
  77. Xfile=${3:-file?}
  78. X
  79. Xcase $# in
  80. X3)    if [ -d "$file" ]; then
  81. X        type=Directory
  82. X    else
  83. X        type=File
  84. X    fi
  85. X    echo "
  86. X$pgm: $severity:
  87. X    $type "\"$file\"": $msg
  88. X"
  89. X    ;;
  90. X*)    echo "
  91. X$pgm: $severity:
  92. X    $msg
  93. X"
  94. X    ;;
  95. Xesac
  96. X
  97. Xexit 1
  98. X
  99. X# 06-Aug-1989: placed in the public domain
  100. X#-----------------+-----------------------+-----------------------------
  101. X# James Frew      | frew@crseo.ucsb.edu   | Computer Systems Lab., UCSB
  102. X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA
  103. SHAR_EOF
  104. if test 659 -ne "`wc -c < 'sherror'`"
  105. then
  106.     echo shar: error transmitting "'sherror'" '(should have been 659 characters)'
  107. fi
  108. chmod +x 'sherror'
  109. fi # end of overwriting check
  110. echo shar: extracting "'usage'" '(515 characters)'
  111. if test -f 'usage'
  112. then
  113.     echo shar: will not over-write existing file "'usage'"
  114. else
  115. sed 's/^X//' << \SHAR_EOF > 'usage'
  116. X#!/bin/sh
  117. X
  118. X# usage -- standard usage message for shell scripts
  119. X
  120. Xexec 1>&2
  121. X
  122. Xpgm=`basename ${1:-command?}`
  123. Xargs="${2-args?}"            # note "-" not ":-", in case no args
  124. Xdescription="${3:-description?}"
  125. X
  126. Xecho "
  127. X$pgm -- $description
  128. X
  129. XUsage: $pgm $args
  130. X"
  131. X
  132. Xexit 1
  133. X
  134. X# 06-Aug-1989: placed in the public domain
  135. X#-----------------+-----------------------+-----------------------------
  136. X# James Frew      | frew@crseo.ucsb.edu   | Computer Systems Lab., UCSB
  137. X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA
  138. SHAR_EOF
  139. if test 515 -ne "`wc -c < 'usage'`"
  140. then
  141.     echo shar: error transmitting "'usage'" '(should have been 515 characters)'
  142. fi
  143. chmod +x 'usage'
  144. fi # end of overwriting check
  145. echo shar: extracting "'xch'" '(802 characters)'
  146. if test -f 'xch'
  147. then
  148.     echo shar: will not over-write existing file "'xch'"
  149. else
  150. sed 's/^X//' << \SHAR_EOF > 'xch'
  151. X#!/bin/sh
  152. X
  153. X[ $# = 2 ] || exec usage $0 'name1 name2' 'exchange files or directories'
  154. X
  155. Xfor check do
  156. X    [ -d $check -o -f $check ] ||
  157. X        exec sherror $0 'No such file or directory' $check
  158. X    checkdir=`dirname $check`
  159. X    [ -w $checkdir ] || exec sherror $0 'No write permission' $checkdir
  160. Xdone
  161. X
  162. Xtmp=,`basename $0`$$
  163. X[ -d $tmp -o -f $tmp ] && exec sherror $0 'temporary name already exists' $tmp
  164. X[ -w . ] || exec sherror $0 'No write permission (for temporary name)' .
  165. X
  166. Xset -x
  167. X
  168. Xmv $1 $tmp    || exit 1
  169. Xmv $2 $1    || exit 1
  170. Xmv $tmp $2    || exit 1
  171. X
  172. Xrm -f $tmp
  173. X
  174. X# 06-Aug-1989: placed in the public domain
  175. X#-----------------+-----------------------+-----------------------------
  176. X# James Frew      | frew@crseo.ucsb.edu   | Computer Systems Lab., UCSB
  177. X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA
  178. SHAR_EOF
  179. if test 802 -ne "`wc -c < 'xch'`"
  180. then
  181.     echo shar: error transmitting "'xch'" '(should have been 802 characters)'
  182. fi
  183. chmod +x 'xch'
  184. fi # end of overwriting check
  185. #    End of shell archive
  186. exit 0
  187. #-----------------+-----------------------+-----------------------------
  188. # James Frew      | frew@crseo.ucsb.edu   | Computer Systems Lab., UCSB
  189. # +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA
  190.  
  191.